package com.ling.job;

import com.ling.bean.Subscribe;
import com.ling.service.SubscribeService;
import com.ling.util.SendEmailUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.List;

public class EmailJob{
    @Autowired
    private SubscribeService subscribeService;

    public void execute(){
        System.out.println("开始获取邮件发送列表...");
        List<Subscribe> userEmailList = subscribeService.getUserEmailList(getWeek());
        //循环列表，发送邮件
        for (Subscribe subscribe : userEmailList) {
            System.out.println("正在给:"+subscribe.getEmail()+"发送订阅邮件。。");
            try {
                SendEmailUtil.sendCommon("用户周订阅推送提醒","<div>周邮件订阅服务提醒，请<a href='http://localhost:8080/login'>登录</a>网站浏览。</div>",subscribe.getEmail());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private int getWeek() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DAY_OF_WEEK)-1;//1--7的值,对应：星期日，星期一，星期二，星期三....星期六
    }
}
